package uutalk.netcloud.talk.base.util.thirdpart;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.Nullable;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.request.transition.Transition;

import java.io.File;

//import android.graphics.drawable.Drawable;
//import com.bumptech.glide.Glide;
//import com.bumptech.glide.load.engine.DiskCacheStrategy;
//import com.bumptech.glide.load.engine.DiskCacheStrategy;
//import com.bumptech.glide.request.RequestListener;
//import com.bumptech.glide.request.animation.GlideAnimation;
//import com.bumptech.glide.request.target.SimpleTarget;
//import com.bumptech.glide.request.target.Target;

/**
 * Created by hujie on 16/6/23.
 */
public class ImageLoader {

    public static void loadImage(Context context, String url, ImageView view) {
        if (url == null || url.isEmpty()) {
            return;
        }
        String type = url.matches("http+.*") ? "URL" : "FILE";
        RequestOptions options = new RequestOptions()
                .centerCrop()
                .priority(Priority.HIGH)
                .circleCrop()
                .diskCacheStrategy(DiskCacheStrategy.NONE);
        if ("URL".equals(type)) {
            Glide.with(context).load(url).apply(options).transition(new DrawableTransitionOptions().crossFade()).into(view);
        } else {
            Glide.with(context).load(new File(url)).apply(options).transition(new DrawableTransitionOptions().crossFade()).into(view);
        }
    }

    /**
     * 获取图片的File句柄
     * @param context
     * @param url
     */
    public static void loadImageFile(Context context, String url, final ImageLoadListener imageLoadListener) {
        if (url == null || url.isEmpty()) {
            return;
        }
        String type = url.matches("http+.*") ? "URL" : "FILE";
        if ("URL".equals(type)) {

            Glide.with(context).load(url).downloadOnly(new SimpleTarget<File>() {
                @Override
                public void onResourceReady(File resource, Transition<? super File> transition) {
                    imageLoadListener.onLoadComplete(resource);
                }

//                @Override
//                public void onResourceReady(File resource, GlideAnimation<? super File> glideAnimation) {
//                    imageLoadListener.onLoadComplete(resource);
//                }
//
//                @Override
//                public void onLoadFailed(Exception e, Drawable errorDrawable) {
//                    imageLoadListener.onLoadFail();
//                }
            });
        } else {
            imageLoadListener.onLoadComplete(new File(url));
        }

    }

    public static void loadImageBitmap(Context context, String url, final ImageLoadListener imageLoadListener) {
        if (url == null || url.isEmpty()) {
            return;
        }
        String type = url.matches("http+.*") ? "URL" : "FILE";
        RequestOptions options = new RequestOptions()
                .centerCrop()
                .priority(Priority.HIGH)
                .circleCrop()
                .diskCacheStrategy(DiskCacheStrategy.ALL);
        if ("URL".equals(type)) {
            Glide.with(context).asBitmap().load(url).listener(new RequestListener<Bitmap>() {
                @Override
                public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
                    imageLoadListener.onLoadFail();
                    return false;
                }

                @Override
                public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
                    imageLoadListener.onLoadComplete(resource);
                    return false;
                }
            }).into(80, 80);;
        } else {
            Glide.with(context).asBitmap().load(new File(url)).listener(new RequestListener<Bitmap>() {
                @Override
                public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
                    imageLoadListener.onLoadFail();
                    return false;
                }

                @Override
                public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
                    imageLoadListener.onLoadComplete(resource);
                    return false;
                }
            }).into(80, 80);
        }
    }

}
